import java.math.BigInteger;
import java.security.SecureRandom;

class RsaDef
{
  private BigInteger n, d, e;

  public RsaDef(int bitlen)
  {
    SecureRandom r = new SecureRandom();
    BigInteger p = new BigInteger(bitlen / 2, 100, r);
    BigInteger q = new BigInteger(bitlen / 2, 100, r);
    n = p.multiply(q);
    BigInteger m = (p.subtract(BigInteger.ONE))
                   .multiply(q.subtract(BigInteger.ONE));
    e = new BigInteger("3");
    while(m.gcd(e).intValue() > 1) e = e.add(new BigInteger("2"));
    d = e.modInverse(m);
  }
  public BigInteger encrypt(BigInteger message)
  {
    return message.modPow(e, n);
  }
  public BigInteger decrypt(BigInteger message)
  {
    return message.modPow(d, n);
  }
}
class rsa
{
	public static void main(String[] args)
	{
		RsaDef r=new RsaDef(8);
		BigInteger big = new BigInteger("12");
		BigInteger en = r.encrypt(big);
		System.out.println("Encrypted number of 12 is \t:"+en);
		BigInteger de = r.decrypt(big);
		System.out.println("Decrypted number of 12 is \t:"+de);
		if (big.compareTo(de) == 0)
			System.out.println("Success!");
		else 
			System.out.println("Not Equal");
	}
}

Output:-
--------
C:\jdk1.3\bin>java rsa
Encrypted number of 12 is       :64
Decrypted number of 12 is       :38
Not Equal

C:\jdk1.3\bin>java rsa
Encrypted number of 12 is       :34
Decrypted number of 12 is       :12
Success!

C:\jdk1.3\bin>